package com.graduation.system.controller;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


import com.graduation.system.model.SysUser;

import cn.circle.commom.dao.IBaseDao;
import cn.circle.commom.dao.impl.BaseDaoImpl;
import com.graduation.system.model.SysCustomer;
import com.graduation.system.model.SysDepartment;
import com.graduation.system.model.SysLostAndFound;
import com.graduation.system.model.SysNotice;

import cn.circle.system.service.ISysLostAndFoundService;
import cn.circle.system.service.ISysNoticeService;
import cn.circle.system.util.DataGrid;
import cn.circle.system.util.MD5;
import cn.circle.system.util.PageHelper;
import cn.circle.system.util.ReturnInfo;



@Controller
@EnableWebMvc
@Repository
public class MainController extends BaseDaoImpl<SysUser>{

	
	@Autowired
	protected SessionFactory sessionFactory;
	@Autowired
	private ISysLostAndFoundService sysLostAndFoundService;
	@Autowired
	private ISysNoticeService sysNoticeService;
	
	/*
	 * 根目录请求
	 */
	@RequestMapping(value = "/")
	public String index() {
		return "/WEB-INF/jsp/system/NewLogin.jsp";
	}
	/*
	 * index请求
	 */
	@RequestMapping(value = "/index")
	public String index2() {
		return "/WEB-INF/jsp/system/NewLogin.jsp";
	}
	/**
	 * 首页请求
	 */
//	@RequestMapping(value = "/manage/goHomePage")
//	public String goHomePage(SysNotice c,Map<String,Object> map,PageHelper ph) {
//		DataGrid dg = sysLostAndFoundService.datagrid();
//		SysLostAndFound l = (SysLostAndFound) dg.getRows().get(0);
//
//		dg = sysNoticeService.datagrid(ph, c, null, null);
//		int i = dg.getRows().size();
//		SysNotice n = (SysNotice) dg.getRows().get(i-1);
//		
//		map.put("model", l);
//		map.put("notice", n);
//		return "/WEB-INF/jsp/system/HomePage.jsp";
//	}
	/*
	 * 主页请求
	 */
	@RequestMapping(value = "/manage/index")
	public String managePage(String dep) {
		System.out.println(dep);
		if(dep.equals("1")){
			return "/WEB-INF/jsp/mobile/main/customerMobile/indexMobile.jsp";
		}else if(dep.equals("2")){
			return "/WEB-INF/jsp/system/system/main.jsp";
		}else{
			return "/WEB-INF/jsp/system/system/main.jsp";
		}
	}
	/*
	 * 跳转到注册页请求
	 */
	@RequestMapping(value = "/manage/goRegister")
	public String goRegister() {
		//return "/WEB-INF/jsp/system/NewRegister.jsp";
		return "/WEB-INF/jsp/system/register.jsp";
	}
	/**
	 * 响应用户登录请求
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/doLogin")
	public ReturnInfo doLogin(String telephone,Map<String,String> map,String dep, String password, ReturnInfo info, HttpServletRequest request) throws Exception {

		telephone = new String(telephone.getBytes("iso8859-1"),"utf-8");
		password = new String(password.getBytes("iso8859-1"),"utf-8");
		
		if (dep.equals("1")) {
			List<SysUser> l = this.getUser(telephone);
			password = MD5.md5(password);
			if(!l.isEmpty() && l.get(0).getPassWord().equals(password)){
				//将登录用户信息存入session
				HttpSession session = request.getSession();
				session.setAttribute("user", l.get(0));
				info.setSuccess(0);
				info.setRows(1);
				info.setMsg("登录成功！");
			}else{
				info.setSuccess(1);
				info.setMsg("用户名密码有错！");
			}
		} else if(dep.equals("2")) {
			List<SysCustomer> l = this.getCustomer(telephone);
			password = MD5.md5(password);
			System.out.println(password);
			if(!l.isEmpty() && l.get(0).getPassWord().equals(password)){
				//将登录用户信息存入session
				System.out.println(l.get(0).getPassWord());
				HttpSession session = request.getSession();
				session.setAttribute("user", l.get(0));
				info.setSuccess(0);
				info.setRows(2);
				info.setMsg("是管理员");
			}else{
				info.setSuccess(1);
				info.setMsg("用户名密码有错！");
			}
		}else{
			System.out.println("超级管理员");
			List<SysCustomer> l = this.getCustomer(telephone);
			password = MD5.md5(password);
			if(!l.isEmpty() && l.get(0).getPassWord().equals(password)){
				//将登录用户信息存入session
				System.out.println(l.get(0).getPassWord());
				HttpSession session = request.getSession();
				session.setAttribute("user", l.get(0));
				info.setSuccess(0);
				info.setRows(3);
				info.setMsg("是管理员");
			}else{
				info.setSuccess(1);
				info.setMsg("用户名密码有错！");
			}
			
		}
		return info;
	}
	/*
	 * 得到User表的单条数据
	 */
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<SysUser> getUser(String telephone) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where telephone = '"+telephone+"'";
		Query query = super.sessionFactory.openSession().createQuery(hql);
		return query.list();
	}
	/*
	 * 得到Customer表的单条数据
	 */
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<SysCustomer> getCustomer(String phone) {
		// TODO Auto-generated method stub
		String hql = "from SysCustomer where phone = '"+phone+"'";
		Query query = super.sessionFactory.openSession().createQuery(hql);
		return query.list();
	}
//	/*
//	 * 得到Customer表的单条数据
//	 */
//	@ResponseBody
//	@SuppressWarnings("unchecked")
//	public String getDepIcon(String depId) {
//		// TODO Auto-generated method stub
//		String hql = "from SysDepartment where depId = '"+depId+"'";
//		Query query = super.sessionFactory.openSession().createQuery(hql);
//		return query.list();
//	}
	/*
	 * 插入表表
	 */
	public void insert(int i){
		String sql = "insert into SYS_STUDENTNUMBER(STUDENT_CLASS,STUDENT_NUMBER) values('"+"class01"+"','"+i+"')";
		Query query1 = sessionFactory.getCurrentSession().createSQLQuery(sql);
		query1.executeUpdate();
	}
	/*
	 * 注册请求
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/manage/doRegister")
	public ReturnInfo doRegister(@Valid SysUser sysUser, BindingResult b, ReturnInfo result) {

		if (sysUser != null) {
			try {
				super.sessionFactory.openSession().save(sysUser);
				result.setMsg("注册成功！去登录吧~");
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("异常");
				result.setMsg("保存失败！");
				result.setSuccess(1);
			}
		}
		return result;
	}
	
	/*
	 * 插入到注册表
	 */
	public void insertRegister(SysUser i){
		String sql = "insert into SYS_STUDENTNUMBER(STUDENT_CLASS,STUDENT_NUMBER) values('"+"class01"+"','"+i+"')";
		Query query1 = sessionFactory.getCurrentSession().createSQLQuery(sql);
		query1.executeUpdate();
	}
	
}
